<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\basic\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 企业管理
 * @package app\enterprise
 * @author LJQ<245369209@qq.com>
 */
class Enterprise extends Api

{
	protected $wfType = 11;
    /**
   * 企业列表
   * @param string enterpriseId 企业id,如果有只显示企业自己的信息.
   * @return Json
   */

	public function enterpriseList($scope = null,$enterpriseId = null){
		$where = [] ;
		if($scope){
			$where['id'] = ['in',$scope];
		}
		if($enterpriseId){
			$where['id'] = $enterpriseId;
		}
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//企业列表
			$info =collection(model('Enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//企业列表
			$info =collection(model('Enterprise')->where($where)->select())->toArray();
		}
	   	 
	 	//总数
	   	 $count = model('Enterprise')->where($where)->count('id'); 
		
    		if ($info && $count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到企业','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'企业信息','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加企业
   	 public function add($data){

          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Basic/Enterprise');
          $result = $model->change($data);

          if (false != $result) {
               setlog('enterpriseadd','enterprise',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'企业添加成功']);
          } else {
              return json(['code'=>0,'msg'=>'添加失败,请检查']);
          }
      }

   	

   /**

   * 修改企业
   * @param  string enterpriseId 企业id,
   * @return Json
   */
   	public function edit($data){
        
          if (empty($data['id'])) {
              return json(['code'=>0,'msg'=>'企业不存在,请检查']);
          }

          $model = model('Basic/Enterprise');     
          if ($model->change($data)) {
               setlog('Enterpriseupdate','Enterprise',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'企业信息修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'企业信息修改失败,请检查']);
            }
    }
    /**
   *  删除企业
   * @param string enterpriseId 企业id,
   * @param Json
   */
     
    public function del() {
      	$enterpriseId = input('enterpriseId',0,'intval');
        $model = model('Basic/Enterprise');
      	if (empty ( $enterpriseId )||empty(($model->where(array('id'=>$enterpriseId))->find()))) {
           	return  json(['code'=>0,'msg'=>'企业id不存在,请检查']);
        }
        if($model->destroy(['id'=>$enterpriseId]) !== FALSE){
            setlog('Enterprisedel','Enterprise',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'企业删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'企业删除失败,请检查']);
       }
    }

     
   /**
   *   查询企业
   * @param string enterpriseId 企业id
   * @param sting areaId园区片区id
   * @param fullName企业名称
   * @return Json
   */
   	public function search($scope = null,$areaId = null,$fullName = null){
   		$where = [] ;
		
   		if($scope){
   			$where['id'] = ['in',$scope];
   		}
		if($areaId){
			list($position,$area) = explode(',',$areaId); 
			$where['areaId'] = $area;
		}

		if($fullName){
			$where['fullName'] = ['like','%'.$fullName.'%'];
		}
   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//企业列表
			$info =collection(model('Enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//企业列表
			$info =collection(model('Enterprise')->where($where)->select())->toArray();
		}
	 	//总数
	    $count = model('Enterprise')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
         	return json(['code'=>0,'msg'=>'没有找到企业','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'企业信息','count'=>$count,'data'=>$info]);
       	}
      }
      
      /**
       * 获取企业位置
       * @param string $enterpriseId
       * @return Json
       */
      public function getEnterprisePosition($enterpriseId){
      	$position = model('Enterprise')->where('id',$enterpriseId)->value('latitudeLongitude');
      	if($position){
      		return json(['code'=>1,'msg'=>'','data'=>$position]);
      	}else{
      		return json(['code'=>0,'msg'=>'没有对应位置']);
      	}
      }
      
      /**
       * 申请审核
       * @param string $enterpriseId
       */
      public function audit($enterpriseId = null){
      	$workflow = model('Workflow')->where('wfType',$this->wfType)->find();
      	if(empty($workflow)||empty($workflow->steps)){
      		return json(['code'=>0,'msg'=>'无对应审核流程','count'=>'','data'=>'']);
      	}else{
      		$enterprise = model('Enterprise')->get($enterpriseId);
      		if($enterprise->isUpdate(true)->save(['infoCheckStatus'=>2]) !== false){
      			$data['title'] = $enterprise['shortname'];
      			$data['wfFlowInfoId'] = $workflow['id'];
      			$data['wfType'] = $this->wfType;
      			$data['busiId'] = $enterpriseId;
      			$data['enterpriseId'] = $enterprise['id'];
      			$data['applyPerson'] = session('user_auth.id');
      			if(model('Wfitem')->save($data)){
      				return json(['code'=>1,'msg'=>'申请成功','count'=>'','data'=>'']);
      			}else{
      				return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      			}
      		}
      	}
      	return json(['code'=>0,'msg'=>'申请失败','count'=>'','data'=>'']);
      
      }
}